<?php 
session_start();
require_once ("jpgraph/jpgraph.php");
require_once ("jpgraph/jpgraph_scatter.php");
include "config.php";

 //Datenbank
$db = new mysqli($mysqlhost, $mysqluser, $mysqlpw, $mysqlname);
$abfrage_davor = 'SELECT datentime,bz FROM bz_werte_'.$_SESSION['id'].' WHERE datentime LIKE "'.date('Ymd',mktime(0,0,0,substr($_GET['d'],-4,2),substr($_GET['d'],-2)-1,substr($_GET['d'],0,4))).'%" ORDER BY datentime DESC';
$abfrage = 'SELECT datentime,bz FROM bz_werte_'.$_SESSION['id'].' WHERE datentime LIKE "'.$_GET['d'].'%" ORDER BY datentime ASC';
$abfrage_danach = 'SELECT datentime,bz FROM bz_werte_'.$_SESSION['id'].' WHERE datentime LIKE "'.date('Ymd',mktime(0,0,0,substr($_GET['d'],-4,2),substr($_GET['d'],-2)+1,substr($_GET['d'],0,4))).'%" ORDER BY datentime ASC';

function  TimeCallback ( $aVal ) {
    return  '';
}

$datax = array();
$datay = array();
$xmin = mktime(0,0,0,substr($_GET['d'],-4,2),substr($_GET['d'],-2),substr($_GET['d'],0,4));
$xmax = mktime(0,0,0,substr($_GET['d'],-4,2),substr($_GET['d'],-2)+1,substr($_GET['d'],0,4));
 
//Gib mir den letzten Wert des Vortages aus
$entries = $db->query($abfrage_davor);
while($entryrow = $entries->fetch_assoc())
{
  if($entryrow['bz'] > 0)
  {
    $datax[] = mktime(substr($entryrow['datentime'],-4,2),substr($entryrow['datentime'],-2,2),0,substr($entryrow['datentime'],4,2),substr($entryrow['datentime'],6,2),substr($entryrow['datentime'],0,4));
    $datay[] = $entryrow['bz'];
    break;
  }   
} 

//Gib mir die Werte des aktuellen Tages aus
$entries = $db->query($abfrage);
if(!$entries)
{
  echo "Datenbankfehler!";
}

while($entryrow = $entries->fetch_assoc())
  {
    if($entryrow['bz'] != "0")
    {
      $datax[] = mktime(substr($entryrow['datentime'],-4,2),substr($entryrow['datentime'],-2,2),0,substr($entryrow['datentime'],4,2),substr($entryrow['datentime'],6,2),substr($entryrow['datentime'],0,4));
      $datay[] = $entryrow['bz'];
    }   
  }
  
//Gib mir den ersten Wert des folgenden Tages aus
$entries = $db->query($abfrage_danach);
while($entryrow = $entries->fetch_assoc())
{
  if($entryrow['bz'] > 0)
  {
    $datax[] = mktime(substr($entryrow['datentime'],-4,2),substr($entryrow['datentime'],-2,2),0,substr($entryrow['datentime'],4,2),substr($entryrow['datentime'],6,2),substr($entryrow['datentime'],0,4));
    $datay[] = $entryrow['bz'];
    break;
  }   
}
 
$graph = new Graph(200,100);
$graph->SetClipping(true);
$graph->SetMarginColor('white');
$graph->SetFrame(false);



$graph->SetScale("linlin",50,300,$xmin,$xmax);
$graph->xaxis->scale->ticks->Set(7200,1800);

 
$graph->img->SetMargin(0,0,0,0);
 
//$graph->title->Set("Blutzuckerverlauf vom ".substr($_GET['d'],-2).".".substr($_GET['d'],-4,2).".".substr($_GET['d'],0,4));
//$graph->title->SetFont(FF_FONT1,FS_BOLD);

$graph->xaxis->SetLabelFormatCallback('TimeCallback');
//$graph->xaxis->SetLabelAngle(90);

$graph->xgrid->Show();

$graph->SetBackgroundImage('images/graph_bg.jpg',BGIMG_FILLPLOT);
$graph->SetBackgroundImageMix(100);


$sp1 = new ScatterPlot($datay,$datax);

$sp1->link->Show();

$sp1->link->SetWeight(1);

$sp1->mark->SetType(MARK_FILLEDCIRCLE);
$sp1->mark->SetFillColor('black');
$sp1->mark->SetWidth(0);
 
$graph->Add($sp1);
$graph->Stroke();

//
 
?>  

